package com.coolgor.axnut.service.airuc.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.airuc.LessonDao;
import com.coolgor.axnut.dto.airuc.LessonExecution;
import com.coolgor.axnut.entity.airuc.Lesson;
import com.coolgor.axnut.enums.airuc.LessonStateEnum;
import com.coolgor.axnut.exceptions.airuc.LessonOperationException;
import com.coolgor.axnut.service.airuc.LessonService;
import com.coolgor.axnut.util.PageCalculator;

@Service
public class LessonServiceImpl implements LessonService {
	private static Logger log = LoggerFactory.getLogger(LessonServiceImpl.class);
	
	@Autowired
	private LessonDao lessonDao;
	
	
	@Override
	public Lesson getLesson(Long lessonID) {
		return lessonDao.queryLessonById(lessonID);
	}

	@Override
	public LessonExecution getLessonList(Lesson lessonCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Lesson> lessonList = lessonDao.queryLessonList(lessonCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = lessonDao.queryLessonCount(lessonCondition);
		LessonExecution result = new LessonExecution();
		if (lessonList != null) {
			result.setState(LessonStateEnum.SUCCESS.getState());
			result.setStateInfo(LessonStateEnum.SUCCESS.getStateInfo());
			result.setLessonList(lessonList);
			result.setCount(count);
		} else {
			result.setState(LessonStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public LessonExecution addLesson(Lesson lesson) {
		// 空值判断
		if (lesson == null) {
			return new LessonExecution(LessonStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 赋初始值
			lesson.setCreateTime(new Date());
			lesson.setLastEditTime(new Date());
			// 添加
			effectedNum = lessonDao.insertLesson(lesson);
			if (effectedNum <= 0) {
				throw new LessonOperationException("课程信息创建失败");
			} 
		} catch (Exception e) {
			throw new LessonOperationException("addLesson error:" + e.getMessage());
		}
		return new LessonExecution(LessonStateEnum.SUCCESS, lesson);
	}

	@Override
	@Transactional
	public LessonExecution modifyLesson(Lesson lesson) {
		// 空值判断
		if (lesson == null) {
			return new LessonExecution(LessonStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 赋初始值
			lesson.setLastEditTime(new Date());
			// 更新操作
			effectedNum = lessonDao.updateLesson(lesson);
			if (effectedNum <= 0) {
				throw new LessonOperationException("课程信息更新失败");
			} 
		} catch (Exception e) {
			throw new LessonOperationException("Modify Lesson Information error:" + e.getMessage());
		}
		return new LessonExecution(LessonStateEnum.SUCCESS, lesson);
	}

	@Override
	@Transactional
	public LessonExecution removeLesson(Lesson lesson) {
		// 空值判断
		if (lesson == null) {
			return new LessonExecution(LessonStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = lessonDao.deleteLesson(lesson);
			if (effectedNum <= 0) {
				throw new LessonOperationException("课程信息删除失败");
			} 
		} catch (Exception e) {
			throw new LessonOperationException("Delete Lesson Information error:" + e.getMessage());
		}
		return new LessonExecution(LessonStateEnum.SUCCESS, lesson);
	}

	@Override
	@Transactional
	public LessonExecution batchRemoveLesson(List<Lesson> lessonList) {
		// 空值判断
		if (lessonList == null) {
			return new LessonExecution(LessonStateEnum.NULL);
		}
		List<Long> lessonIDList = new ArrayList<Long>();
		for (int i=0; i<lessonList.size(); i++) {
			lessonIDList.add(lessonList.get(i).getLessonID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = lessonDao.batchDeleteLesson(lessonIDList);
			if (effectedNum <= 0) {
				throw new LessonOperationException("课程信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new LessonOperationException("Batch delete Lesson Information error:" + e.getMessage());
		}
		LessonExecution ce = new LessonExecution(LessonStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		ce.setCount(effectedNum);
		return ce;
	}

}
